﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace AnOtherBlog.Core.Web
{
	/// <summary>
	/// Admin Web Page base class
	/// </summary>
	public abstract class AdminBasePage : System.Web.UI.Page
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPreInit(EventArgs e)
		{
			base.OnPreInit(e);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
		}

		#region Messages
		/* Esta es la versión vieja

		/// <summary>
		/// Reference to Label lblMsg
		/// </summary>
		private Label _lblMsg;
		public Label LblMsg
		{
			get { return _lblMsg; }
			set { _lblMsg = value; }
		}

		/// <summary>
		/// Show a message
		/// </summary>
		/// <param name="msg">Message text.</param>
		protected void MsgSet(String msg)
		{
			if (_lblMsg != null)
			{
				_lblMsg.Text = "&nbsp;" + msg + "&nbsp;";
			}
		}
		/// <summary>
		/// Show a message preceded with critial error 
		/// </summary>
		/// <param name="msg">Message text.</param>
		protected void MsgSetCritical(String msg)
		{
			MsgSet(string.Format("Error cr&iacute;tico: {0}", msg));
		}
		/// <summary>
		/// Show a success message
		/// </summary>
		/// <param name="msg">Message text.</param>
		protected void MsgSetOk(String msg)
		{
			if (_lblMsg != null)
			{
				_lblMsg.BackColor = System.Drawing.Color.Aqua;
				_lblMsg.Text = msg;
			}
		}
*/
		#endregion

		#region Messages like GMail

		/// <summary>
		/// Reference to Label lblMsg in Master Page
		/// </summary>
		private Label _lblMsg;
		/// <summary>
		/// Reference to Timer timerMsg in Master Page
		/// </summary>
		private Timer _timerMsg;

		/// <summary>
		/// Test if exist reference for lblMsg in Master Page
		/// also it test if exist reference for timerMsg in Master Pager too
		/// </summary>
		private Boolean MsgIsOk
		{
			get
			{
				this._lblMsg = (Label) this.Master.FindControl("lblMsg");
				this._timerMsg = (Timer) this.Master.FindControl("timerMsg");
				return (this._lblMsg != null);
			}
		}
		/// <summary>
		/// Set timer interval in miliseconds
		/// </summary>
		/// <param name="interval"></param>
		private void MsgSetTimer(int interval)
		{
			if (this._timerMsg != null)
			{
				_timerMsg.Interval = interval;
			}
		}
		/// <summary>
		/// Shows messages with partial postback
		/// </summary>
		private void MsgShow()
		{
			UpdatePanel updpnlMsg = (UpdatePanel) this.Master.FindControl("updpnlMsg");
			if (updpnlMsg != null)
			{
				updpnlMsg.Update();
			}
		}
		/// <summary>
		/// Show a message
		/// </summary>
		/// <param name="msg">Message text to put in Label control.</param>
		protected virtual void MsgSet(string msg)
		{
			if (MsgIsOk)
			{
				MsgSetTimer(10000);
				_lblMsg.Text = "&nbsp;" + msg + "&nbsp;";
				MsgShow();
			}
		}
		/// <summary>
		/// Show a message preceded with critial error 
		/// </summary>
		/// <param name="msg">Message text to put in Label control</param>
		protected virtual void MsgSetCritical(string msg)
		{
			if (MsgIsOk)
			{
				MsgSetTimer(3600000);
				_lblMsg.Text = "&nbsp;" + string.Format("Error cr&iacute;tico: {0}", msg) + "&nbsp;";
				_lblMsg.ForeColor = System.Drawing.Color.Red;
				MsgShow();
			}
		}

		#endregion



	}
}
